
package hk.ces.oxfam.patch.connection;

import hk.ces.oxfam.patch.config.Config;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;

/**
 *
 * @author ReutyCheng
 */
public class ConnectionFactory
{
  private ConnectionFactory()
  {
  }
  
  public static Connection openConnection()
  {
    Connection conn = null;

    try
    {
      Class.forName(Config.CLASS_NAME.toString());
      conn = DriverManager.getConnection(Config.URL.toString(), Config.USER.toString(), Config.PASSWORD.toString());
    }
    catch (SQLException ex)
    {
      Logger.getLogger(ConnectionFactory.class.getName()).error(ex.getMessage(), ex);
    }
    catch (ClassNotFoundException ex)
    {
      Logger.getLogger(ConnectionFactory.class.getName()).error(ex.getMessage(), ex);
    }

    return conn;
  }

  public static void closeConnection(Connection connection)
  {
    try
    {
      if (connection != null)
      {
        connection.close();
      }
    }
    catch (SQLException ex)
    {
      Logger.getLogger(ConnectionFactory.class.getName()).error(ex.getMessage(), ex);
    }
  }
}
